package com.zzptc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.zzptc.mzb.myapplication.Bean.User;

import org.litepal.LitePal;

import java.util.List;

public class LoginMain2Activity extends AppCompatActivity {
    private EditText ame, mima;
    private Button deng, zhu;
    private RadioGroup radioGroup;
    private Button  registerbtn,loginbtn;
    int CHOOSE_LEVEL=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main2);
        User user=new User(0,"damin","mzb",2);
        user.save();
        initView();
        setListener();
    }

    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.userrdb:
                        CHOOSE_LEVEL=1;
                        break;
                    case  R.id.adminrdb:
                        CHOOSE_LEVEL=2;
                        break;
                }
            }
        });
        //登入按钮模块
       deng.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View view) {
               String zh = ame.getText().toString().trim();
               String mm = mima.getText().toString().trim();
               User user=confirmUser(zh,mm);//还回账号和密码
               if(user==null){
                   showToast("账号密码错误");
              }else  if (user.getPermission()==CHOOSE_LEVEL) {
                   SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                   Intent intent = new Intent(LoginMain2Activity.this, MainActivity.class);
                   SharedPreferences.Editor editor = sp.edit();
                   editor.putInt("userid", user.getId());
                   editor.putString("username", user.getName());
                   editor.putString("password", user.getPassword());
                   editor.putInt("permissio n",user.getPermission());
                   editor.apply();
                   Intent intent1 = new Intent(LoginMain2Activity.this, MainActivity.class);
                           startActivity(intent);
               }else {
                   showToast("权限选择错误");
               }
          }
       });
        zhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String zh = ame.getText().toString().trim();
                String mm = mima.getText().toString().trim();
                User user=new User(0,zh,mm);
                if (user.save()){
                    showToast("注册成功");
                }else {
                    Intent intent=new Intent(LoginMain2Activity.this,MainActivity.class);
                    startActivity(intent);
                    //showToast("该用户已经存在，请登录");
                }
            }
        });
    }
    private User confirmUser(String zh, String mm) {
        List<User> userList = LitePal.findAll(User.class);
        for (User u : userList) {
            if (zh.equals(u.getName()) && mm.equals(u.getPassword())) {
                return u;//打断循环
            }
        }
        return  null;
    }
    private void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
    private void initView() {
        ame=findViewById(R.id.ame);
       mima=findViewById(R.id.mima);
        deng=findViewById(R.id.deng);
       zhu=findViewById(R.id.zhu);
       radioGroup=findViewById(R.id.radioGroup);
    }

}
